package com.example.binpacking.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.binpacking.model.QuestionnaireItem;
import com.example.binpacking.model.QuestionnaireName;
import com.example.binpacking.model.Result;
import com.example.binpacking.model.dto.QuestionItemPageDto;
import com.example.binpacking.model.dto.QuestionNameItemOptionMDDto;
import com.example.binpacking.service.QuestionnaireItemService;
import com.example.binpacking.service.QuestionnaireNameService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/pelicanpsyc/api")
public class QuestionnaireItemController {
    @Autowired
    private QuestionnaireItemService questionnaireItemService;
    @ApiOperation("分页并进行获得调查问卷条款")
    @PostMapping("/getQuestionnaireNameItem")
    public Result getQuestionnaireNameItem(@RequestBody QuestionItemPageDto questionItemPageDto) {
        Page<QuestionnaireItem> questionnaireItem = questionnaireItemService.selectByDeletedAndParent(questionItemPageDto.getParentid(),questionItemPageDto.getIsdeleted(),questionItemPageDto.getCurrentpage(),questionItemPageDto.getPagesize());
        return Result.success(questionnaireItem);
    }
    @ApiOperation("插入调查问卷条款")
    @PostMapping("/insertQuestionnaireItem")
    public Result insertQuestionnaireItem(@RequestBody QuestionnaireItem questionnaireItem) {
        boolean isSuccess = questionnaireItemService.save(questionnaireItem);
        if (isSuccess) {
            return Result.success(questionnaireItem);  // 返回传入的对象
        } else {
            return Result.fail("Failed to save questionnaire name.");
        }
    }
    @ApiOperation("修改调查问卷条款")
    @PostMapping ("/modifyQuestionnaireItem")
    public Result modifyQuestionnaireItem(@RequestBody QuestionNameItemOptionMDDto questionNameItemOptionMDDto) {
        int i =questionnaireItemService.updateNameById(questionNameItemOptionMDDto.getId(),questionNameItemOptionMDDto.getName());
        if(i!=0){
            return Result.success("y");
        }else {
            return Result.fail("n");
        }
    }
    @ApiOperation("删除调查问卷条款(软删除)")
    @PostMapping ("/deleteQuestionnaireItem")
    public Result deleteQuestionnaireItem(@RequestBody QuestionNameItemOptionMDDto questionNameItemOptionMDDto) {
        int i =questionnaireItemService.updateDeletedById(questionNameItemOptionMDDto.getId(),questionNameItemOptionMDDto.getIsdeleted());
        if(i!=0){
            return Result.success("y");
        }else {
            return Result.fail("n");
        }
    }

}
